package com.hrma.services.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hrma.dao.EmployeeMapper;
import com.hrma.dao.PenaltyMapper;
import com.hrma.entity.Penalty;
import com.hrma.entity.PenaltyDTO;
import com.hrma.entity.SelectResult;
import com.hrma.services.PenaltyService;
import com.hrma.utils.UserUtil;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PenaltyImpl implements PenaltyService {
    private final PenaltyMapper penaltyMapper;
    private final EmployeeMapper employeeMapper;

    public PenaltyImpl(PenaltyMapper penaltyMapper, EmployeeMapper employeeMapper) {
        this.penaltyMapper = penaltyMapper;
        this.employeeMapper = employeeMapper;
    }

    @Override
    public Penalty add(Penalty data) {
        data.setAuditor(employeeMapper.getNumById(UserUtil.getUserId()));
        penaltyMapper.add(data);
        return data;
    }

    @Override
    public void delete(int id) {
        if (!penaltyMapper.exist(id)) {
            throw new RuntimeException("id字段不存在！");
        }
        penaltyMapper.delete(id);
    }

    @Override
    public void update(Penalty data) {
        if (!penaltyMapper.exist(data.getId())) {
            throw new RuntimeException("id字段不存在！");
        }
        penaltyMapper.update(data);
    }

    @Override
    public List<SelectResult> select() {
        return penaltyMapper.select();
    }

    @Override
    public Page<Penalty> getList(PenaltyDTO data) {
        data.setState(1 );
        PageHelper.startPage(data.getPageNum(),data.getPageSize());
        return penaltyMapper.getList(data);
    }
}
